package com.rpframework.module.common.bottom.api.juhe.cn;

import com.rpframework.core.json.FailException;

/**
 * JSON格式
 * Created by jump on 2016/8/9.
 */
class Json<T> {
    private Integer resultcode;
    private String resultinfo;

    private Row<T> row;

    public Row<T> getRow() {
        return row;
    }

    public void setRow(Row<T> row) {
        this.row = row;
    }

    private Integer getResultcode() {
        return resultcode;
    }

    public void setResultcode(Integer resultcode) {
        this.resultcode = resultcode;
    }

    private String getResultinfo() {
        return resultinfo;
    }

    public void setResultinfo(String resultinfo) {
        this.resultinfo = resultinfo;
    }

    public class Row<E> {
        private String status;
        private E result;

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public E getResult() {
            return result;
        }

        public void setResult(E result) {
            this.result = result;
        }
    }

    /**
     * 是否成功返回
     *
     * @param json JSON
     * @param <T>  结果集
     * @return OK=true
     */
    private static <T> boolean isSuccess(Json<T> json) {
        return json.getResultcode() == 1;
    }

    /**
     * 是否成功返回
     *
     * @param json JSON
     * @param <T>  结果集
     */
    public static <T> void success(Json<T> json) {
        if (!isSuccess(json)) {
            throw new FailException(json.getResultinfo());
        }
    }
}
